package com.coldworks.base.manager;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.AdapterView.OnItemSelectedListener;

import com.coldworks.base.ui.BaseGallery;
import com.coldworks.base.util.BaseCONST;
import com.coldworks.base.util.BaseGuideAdapter;

/**
 * 本地配置管理器
 * 
 * @since 2012-07-07
 */
public class BaseGuideManager {
	protected final Logger log = LoggerFactory.getLogger(this.getClass());
	private static BaseGuideManager instance = null;
	private int nowPosition;

	public static BaseGuideManager getInstance() {
		if (instance == null) {
			synchronized (BaseGuideManager.class) {
				if (instance == null) {
					instance = new BaseGuideManager();
				}
			}
		}
		return instance;
	}

	private BaseGuideManager() {
	}

	public BaseGallery getGuideView(Context context, final Handler handler) {

		List<Integer> guides = new ArrayList<Integer>();
		// guides.add(R.drawable.guide0);
		// guides.add(R.drawable.guide1);
		BaseGuideAdapter adp = new BaseGuideAdapter(context, guides);

		final BaseGallery guideGallery = new BaseGallery(context, null);
		FrameLayout.LayoutParams glp = new FrameLayout.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.FILL_PARENT);
		glp.gravity = Gravity.CENTER;
		guideGallery.setLayoutParams(glp);
		guideGallery.setUnselectedAlpha(255);
		guideGallery.setVisibility(View.VISIBLE);
		guideGallery.setAdapter(adp);
		guideGallery.setFadingEdgeLength(0);
		guideGallery.setSpacing(0);
		guideGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				nowPosition = position;

				if (position == 1) {
					log.info(String.valueOf(position));
					guideGallery.setVisibility(View.GONE);
					guideGallery.setTag(null);
					Message msg = new Message();
					msg.what = BaseCONST.OP.GUIDE_DONE;
					msg.setTarget(handler);
					msg.sendToTarget();
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		guideGallery
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {

						if (nowPosition == 0) {
							guideGallery.setVisibility(View.GONE);
							guideGallery.setTag(null);
							Message msg = new Message();
							msg.what = BaseCONST.OP.SPLASH_GONE;
							msg.setTarget(handler);
							msg.sendToTarget();
						} else {
							guideGallery.setSelection(nowPosition + 1);
						}

					}
				});
		return guideGallery;
	}
}
